//
//  TestProblemsViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/11.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "TestProblemsViewController.h"
#import "TestProblemsTableViewCell.h"
#import "TestAnswerTableViewCell.h"

@interface TestProblemsViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,weak) UIButton *selctButton;
@property (nonatomic,assign) NSInteger slectIndex;
@end

@implementation TestProblemsViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"趣味测试题";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TestProblemsTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"TestProblemsTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TestAnswerTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"TestAnswerTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.slectIndex = 100;
    [self creatDataList];
    [self creatHeadView];
}
#pragma mark - 请求数据
- (void)creatDataList{
    for (int i=0; i<3; i++) {
        NSMutableDictionary *replaydic=[NSMutableDictionary dictionary];
        replaydic[@"proContent"]=@"教育孩子";
        replaydic[@"answerSelect"] = i == 0 ? @"A" : @"B";
        replaydic[@"answerContent"] = i != 0 ? @"思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节思维缜密，因为考虑到很多细节" : @"大方干练大方干练大方干练大方干练大方干练大方干练大方干练大方干练大方干练大方干练大方干练";
        [self.dataArray addObject:[TestProblemsInfo mj_objectWithKeyValues:replaydic]];
    }
}
#pragma mark - creatHeadView
- (void)creatHeadView{
    NSString *proStr = @"有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹有一天餐厅炸弹";
    double proHeight = [Global getSizeOfString:proStr maxWidth:kScreenW - 20 maxHeight:CGFLOAT_MAX withFontSize:15.6].height;
    
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, proHeight + 20);
    view.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = view;
    
    UILabel *proLabel = [[UILabel alloc]init];
    proLabel.frame = CGRectMake(10, 10, kScreenW - 20, proHeight);
    proLabel.font = [UIFont systemFontOfSize:15];
    proLabel.numberOfLines = 0;
    proLabel.text = proStr;
    [view addSubview:proLabel];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ) {
        if (self.slectIndex == 100) {
            return 0;
        }
        return 1;
    }
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        TestAnswerTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TestAnswerTableViewCell" forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.testProblemsInfo = self.dataArray[self.slectIndex];
        return cell;
    }
    TestProblemsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TestProblemsTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.testProblemsInfo = self.dataArray[indexPath.row];
    __weak typeof (self)unself = self;
    __weak typeof (cell)unCell = cell;
    cell.selectAnswerBlock = ^{
        if (unself.slectIndex != indexPath.row) {
            unCell.selImageView.selected = YES;
            unself.selctButton.selected = NO;
            unself.selctButton = unCell.selImageView;
            
            unself.slectIndex = indexPath.row;
            [unself.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
        }
        
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        return [tableView fd_heightForCellWithIdentifier:@"TestAnswerTableViewCell" cacheByIndexPath:indexPath configuration:^(TestAnswerTableViewCell* cell) {
            // 配置 cell 的数据源，和 "cellForRow" 干的事一致，比如：
            cell.testProblemsInfo = self.dataArray[self.slectIndex];
        }];
//        return 100;
    }
    return 32;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
